const assignTask = {
  state: {
    //查找的数据
    searchTaskDetail: [], //查找任务详情
    searchTaskDays: [], //查找该任务哪些天包含 ['taskFirstDay','taskSecondDay']格式
    tmpTaskData: [], //回显、添加课件数据
    max_sort:0, //当前子任务最大排序值
    daysData: [
      { day_name: '第1天', id: 'taskFirstDay' },
      { day_name: '第2天', id: 'taskSecondDay' },
      { day_name: '第3天', id: 'taskThirdDay' },
      { day_name: '第4天', id: 'taskFourthDay' },
      { day_name: '第5天', id: 'taskFifthDay' },
      { day_name: '第6天', id: 'taskSixthDay' },
    ],
    taskDays: {
      taskFirstDay: {
        listen: [],
        read: [],
        record: [],
        create: [],
      },
      taskSecondDay: {
        listen: [],
        read: [],
        record: [],
        create: [],
      },
      taskThirdDay: {
        listen: [],
        read: [],
        record: [],
        create: [],
      },
      taskFourthDay: {
        listen: [],
        read: [],
        record: [],
        create: [],
      },
      taskFifthDay: {
        listen: [],
        read: [],
        record: [],
        create: [],
      },
      taskSixthDay: {
        listen: [],
        read: [],
        record: [],
        create: [],
      },
    },
  },
  mutations: {
    //添加编辑课件 taskFirstDay 含阅读、听力、录音、创作  task_type: read、listen、record、create
    //阅读、听力、录音 {"id":1,"title":"ceshi","task_type":"read","book_type":243,"cover_url":""}
    //创作 {"id":1,"title":"ceshi","task_type":"create","cover_url":[{"id":1,"cover_url":""}]}
    EDIT_TASK_DAY(state, task) {
      console.log('------EDIT_TASK_DAY1------', task)

      if (state.taskDays[task.current_day][task.task_type].length > 0) {
        let position = 0
        for (
          let i = 0;
          i < state.taskDays[task.current_day][task.task_type].length;
          i++
        ) {
          let tmp = state.taskDays[task.current_day][task.task_type][i]
          if (tmp.id == task.id && tmp.content_id == task.content_id) {
            //先删除
            state.taskDays[task.current_day][task.task_type].splice(i, 1) //删除指定键值
            position = i
            break
          }
        }

        console.log('------------')
        state.taskDays[task.current_day][task.task_type].unshift(task)
        // state.taskDays[task.current_day][task.task_type].splice(position, 0, task);   //指定位置添加数组元素
      } else {
        console.log('-----666666-------')
        state.taskDays[task.current_day][task.task_type].unshift(task)
        // state.taskDays[task.current_day][task.task_type].push(task);
      }
    },
    //获取编辑任务详情  若编辑涉及到更换文件可以先删除在添加
    SEARCH_TASK_Detail(state, task) {
      console.log('SEARCH_TASK_Detail', task)
      if (state.taskDays[task.current_day][task.task_type].length > 0) {
        //听力、阅读、录音、创作
        for (
          let i = 0;
          i < state.taskDays[task.current_day][task.task_type].length;
          i++
        ) {
          let tmp = state.taskDays[task.current_day][task.task_type][i]
          //let tmp = JSON.parse(state.taskDays[task.current_day][task.task_type][i]);
          if (tmp.id == task.id) {
            state.searchTaskDetail =
              state.taskDays[task.current_day][task.task_type]
            break
          }
        }
      }
    },
    //遍历获取任务同步天数
    SEARCH_SYNC_TASK_DAY(state, task) {
      console.log('SEARCH_SYNC_TASK_DAY', task)
      let data = []
      for (let i = 0; i < state.daysData.length; i++) {
        let search_arr = state.taskDays[state.daysData[i].id][task.task_type]
        for (let j = 0; j < search_arr.length; j++) {
          if (task.id == search_arr[j].id) {
            data.push(state.daysData[i].id) //为 taskFirstDay 格式
          }
        }
      }
      state.searchTaskDays = data
      console.log('SEARCH_SYNC_TASK_DAY result', data)
    },
    //删除课件
    DELETE_TASK_DAY(state, task) {
      console.log('DELETE_TASK_DAY', task)
      if (state.taskDays[task.current_day][task.task_type].length > 0) {
        //听力、阅读、录音、创作
        for (
          let i = 0;
          i < state.taskDays[task.current_day][task.task_type].length;
          i++
        ) {
          let tmp = state.taskDays[task.current_day][task.task_type][i]
          //let tmp = JSON.parse(state.taskDays[task.current_day][task.task_type][i]);
          if (tmp.id == task.id && tmp.content_id == task.content_id) {
            state.taskDays[task.current_day][task.task_type].splice(i, 1)
            break
          }
        }
      }
    },
    //任务模板编辑 课件信息赋值
    SET_TASK_DAY(state, taskInfo) {
      state.taskDays = taskInfo
    },
    //删除临时数据
    DELETE_TMP_TASK_DATA(state, taskDataObj) {
      console.log('---now chuan delete data---', taskDataObj)
      for (let i = 0; i < state.tmpTaskData.length; i++) {
        console.log(
          '---now delete data detail---',
          state.tmpTaskData[i].id + '---' + state.tmpTaskData[i].content_id
        )
        if (
          taskDataObj.id == state.tmpTaskData[i].id &&
          taskDataObj.content_id == state.tmpTaskData[i].content_id
        ) {
          console.log('--del success--', state.tmpTaskData[i])
          state.tmpTaskData.splice(i, 1) //删除指定键值
          break
        }
      }
    },
    //添加临时数据
    SET_TMP_TASK_DATA(state, taskData) {
      state.tmpTaskData.push(taskData)
    },
    //重置临时数据为空 添加、回显课件等
    RESET_TMP_TASK_DATA(state) {
      state.tmpTaskData = []
    },
    //获取当前最大排序值
    GET_TASK_MAX_SORT(state,task){

      console.log('GET_TASK_MAX_SORT', task)
      let max_sort = 0;

      if(state.taskDays[task.current_day]['listen'].length >0){
          for (let i = 0; i < state.taskDays[task.current_day]['listen'].length; i++) {
                if(state.taskDays[task.current_day]['listen'][i].sort > max_sort){
                    max_sort = state.taskDays[task.current_day]['listen'][i].sort;
                }
          }
      }
      if(state.taskDays[task.current_day]['read'].length >0){
        for (let i = 0; i < state.taskDays[task.current_day]['read'].length; i++) {
          if(state.taskDays[task.current_day]['read'][i].sort > max_sort){
            max_sort = state.taskDays[task.current_day]['read'][i].sort;
          }
        }
      }
      if(state.taskDays[task.current_day]['record'].length >0){
        for (let i = 0; i < state.taskDays[task.current_day]['record'].length; i++) {
          if(state.taskDays[task.current_day]['record'][i].sort > max_sort){
            max_sort = state.taskDays[task.current_day]['record'][i].sort;
          }
        }
      }
      if(state.taskDays[task.current_day]['create'].length >0){
        for (let i = 0; i < state.taskDays[task.current_day]['create'].length; i++) {
          if(state.taskDays[task.current_day]['create'][i].sort > max_sort){
            max_sort = state.taskDays[task.current_day]['create'][i].sort;
          }
        }
      }
      state.max_sort = max_sort


    },
    //重置为初始状态
    RESET_ALL_STATES(state) {
      for (let i = 0; i < state.daysData.length; i++) {
        let now_day = state.daysData[i].id
        state.taskDays[now_day].listen = []
        state.taskDays[now_day].read = []
        state.taskDays[now_day].record = []
        state.taskDays[now_day].create = []
      }
    },
  },
  actions: {},
  getters: {},
}
export default assignTask
